﻿using HalconDotNet;
using Halcon二次开发.Controller.数据加载与保存;
using Halcon二次开发.Model;
using Halcon二次开发.Model.自定义控件;
using Halcon二次开发.View.相机;
using Halcon二次开发.View.结果分析工具;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Windows.Forms.DataVisualization.Charting;

namespace Halcon二次开发.View.统计图
{
    public partial class StatisticsForm : Form
    {

        public StatisticsForm()
        {
            InitializeComponent();
        }
        //当前工程对象
        private HwindowFlowNodeUrseControl hwindow;
        //所有窗体对象
        private TreeView treeViews = null;
        //统计窗体对象
        private AnalyseResultForms analyseResultForms;
        //统计图对象
        private Series series1 = new Series();
        private Series series2 = new Series();
        private void StatisticsForm_Load(object sender, EventArgs e)
        {
            series1.Name = "合格";
            series1.Color = Color.Green;
            series2.Name = "不合格";
            series2.Color = Color.Red;
            chart1.Series.Add(series1);
            chart1.Series.Add(series2);
        }
        public void SetNodesUrse(HwindowFlowNodeUrseControl hwindow, TreeView treeViews,HwindowFlowNodeUrseControl hwindowFlow)
        {
            this.hwindow = hwindow;
            this.Text = hwindow.name+"工程统计图界面";
            this.chart1.Titles[0].Text= hwindow.name+"数据图统计";
            this.treeViews = treeViews;
            foreach (TreeNode node in treeViews.Nodes)
            {
                if (node.Text.Contains("测量分析工具"))
                {
                    analyseResultForms = node.Tag as AnalyseResultForms;
                }
            }
        }
        //需要记录总数
        private double numberallnumber;
        //合格数记录
        private double numberOK;
        //不合格数记录
        private double numberNG;
        public void ClickRun(object sender, EventArgs e)
        {
            double numberA = 0;
            double numberB = 0;    
            if (analyseResultForms != null)
            {
                if (analyseResultForms.controller != null)
                {
                    if (analyseResultForms.controller.OutputResults != null)
                    {
                        numberallnumber += analyseResultForms.controller.OutputResults.Length;
                        for (int i = 0; i < analyseResultForms.controller.OutputResults.Length; i++)
                        {
                            if (analyseResultForms.controller.OutputResults[i] == true)
                            {
                                series1.Points.AddXY("合格", 10);
                                numberA++;
                            }
                            else
                            {
                                series2.Points.AddXY("不合格", 5);
                                numberB++;
                            }
                        }
                        numberOK += numberA;
                        numberNG += numberB;
                        double text = (numberOK / numberallnumber) * 100;
                        chart1.Titles[1].Text = "良品率:" + text + "%";
                        chart1.Titles[2].Text = "不良品率:" + (100 - text) + "%";
                    }
                }
            }
            
        }
        private void StatisticsForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            this.Hide();
            e.Cancel = true;
        }
    }
}
